package com.example.shop.fragment;

import android.os.Message;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;

import com.example.shop.Constant;
import com.example.shop.R;
import com.example.shop.activity.LimitBuyActivity;
import com.example.shop.activity.TopicActivity;
import com.example.shop.adapter.ItemLvClasslistHomeFragAdapter;
import com.example.shop.adapter.VpBannerFragHomeItemAdapter;
import com.example.shop.base.SCBaseFragment;
import com.example.shop.entity.HomeEntity;
import com.example.sjlibrary.net.BaseJSONOverListener;
import com.example.sjlibrary.net.BaseNetForJSON;
import com.example.sjlibrary.net.http.HttpForJSONByGet;

import java.util.Timer;
import java.util.TimerTask;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by Administrator on 2016/3/11.
 */
public class HomeFragment extends SCBaseFragment implements AdapterView.OnItemClickListener {

    @Bind(R.id.edt_search_frag_home)
    EditText mEdtSearchFragHome;
    @Bind(R.id.bt_search_frag_home)
    Button mBtSearchFragHome;
    @Bind(R.id.layout_search_home_frag)
    LinearLayout mLayoutSearchHomeFrag;
    @Bind(R.id.vp_banner_home_frag)
    ViewPager mVpBannerHomeFrag;
    @Bind(R.id.lv_classlist_home_frag)
    ListView mLvClasslistHomeFrag;
    @Bind(R.id.bt_call_home_frag)
    Button mBtCallHomeFrag;
    @Bind(R.id.rg_red_point_vp_banner_home_frag)
    RadioGroup mRadioGroup;
    public static boolean isYJG = false;
    private Timer mTimer = new Timer();
    private boolean mUserDragging = false;
    private android.os.Handler mHandler = new android.os.Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            if (msg.what == 10086) {
                if (!mUserDragging) {

                    mVpBannerHomeFrag.setCurrentItem(mVpBannerHomeFrag.getCurrentItem() + 1);
                    updateRedPoint();
                }
            }
        }
    };

    private void updateRedPoint() {
        int nowpos = mVpBannerHomeFrag.getCurrentItem();
        if (mVpBannerFragHomeItemAdapter != null) {
            RadioButton nowRedPoint = (RadioButton) mRadioGroup.getChildAt(nowpos % mVpBannerFragHomeItemAdapter.mPicNums);
            nowRedPoint.setChecked(true);
        }

    }

    @Override
    public View setContentView(ViewGroup container) {
        View conentView = mLayoutInflater.inflate(R.layout.fragment_home, container, false);
        ButterKnife.bind(this, conentView);
        setTitleCenter(R.string.home_title);
        mVpBannerHomeFrag.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                updateRedPoint();

            }

            @Override
            public void onPageScrollStateChanged(int state) {
                if (state == ViewPager.SCROLL_STATE_IDLE) {
                    mUserDragging = false;
                } else {
                    mUserDragging = true;
                }

            }
        });
        BaseNetForJSON baseNetForJSON = new HttpForJSONByGet(Constant.HOME_URL, HomeEntity.class, new HomeNetOverListener());
        baseNetForJSON.netForJSON();
        return conentView;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.unbind(this);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        mTimer.cancel();
    }

    @OnClick({R.id.bt_search_frag_home, R.id.bt_call_home_frag})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.bt_search_frag_home:
                break;
            case R.id.bt_call_home_frag:
                break;
        }
    }

    private VpBannerFragHomeItemAdapter mVpBannerFragHomeItemAdapter;
    private ItemLvClasslistHomeFragAdapter mItemLvClasslistHomeFragAdapter;


    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        switch (position) {
            case 0:
                this.startActivityForResult(LimitBuyActivity.class,10101);
                break;
            case 1:
                this.startActivity(TopicActivity.class);
                break;

            default:

                break;
        }

    }

//    @Override
//    public void onActivityResult(int requestCode, int resultCode, Intent data) {
//        super.onActivityResult(requestCode, resultCode, data);
//        LogUtils.logE("Fragmnet 响应道。。。");
//    }

    private class HomeNetOverListener implements BaseJSONOverListener<HomeEntity> {
        @Override
        public void onError() {
            isYJG = true;
            Toast.makeText(mBaseActivity, "网络访问失败！！", Toast.LENGTH_SHORT).show();
        }

        @Override
        public void onSuccess(HomeEntity entity) {
            isYJG = true;
            if (mVpBannerFragHomeItemAdapter == null) {
                mVpBannerFragHomeItemAdapter = new VpBannerFragHomeItemAdapter(entity.getHome_banner(), mBaseActivity);
            }
            if (mItemLvClasslistHomeFragAdapter == null) {
                mItemLvClasslistHomeFragAdapter = new ItemLvClasslistHomeFragAdapter(mBaseActivity, entity.getHome_classlist());

            }
            mVpBannerHomeFrag.setAdapter(mVpBannerFragHomeItemAdapter);
            mLvClasslistHomeFrag.setAdapter(mItemLvClasslistHomeFragAdapter);
            mLvClasslistHomeFrag.setOnItemClickListener((AdapterView.OnItemClickListener) mBaseFragmentSelf);
            mVpBannerHomeFrag.setCurrentItem(50000);
            mTimer.schedule(new TimerTask() {
                @Override
                public void run() {
                    mHandler.sendEmptyMessage(10086);
                }
            }, 5000, 2000);
        }
    }

}
